!-----------------------------------------------------------------------
!  Copyright (c) 2020 Feasa Enterprises Ltd.
!  Rev 3.0  August 2020
!
!  All Rights Reserved.  Reproduction, adaptation, or translation
!  without prior written permission is prohibited, except as allowed
!  under the copyright laws.
!
!-----------------------------------------------------------------------
!
! Device        : Feasa_20i
! Manufacturer  : Feasa Enterprises Ltd.
! Description   : 20 Channel device to interrogate a LED
! Package       : 20 pin
! Test Platform : 3070
! Safeguard     : cmos_3v3
! Test          : Set / Reset Sync Serial  - Setfactor
!-----------------------------------------------------------------------
! WARNING:  This is a Led Analyser function referenced by a PDL.
!
!   This test is not intended to be used directly for a Led analyser.
!   Instead, a part description library for the Led analyser should be
!   used where the part description language of that file refers to
!   this file as the part number of one of it's several "sub-components" .
!   Notice also that this test uses signal names rather than ordinal
!   pin numbers.  The referencing part description library should map
!   the device's actual pin numbers to these signal names.
!
!
! Additional Comments:
!
!   Test sets the exposure level of the Analyser using Sync Serial.
!
!-----------------------------------------------------------------------
!      Synchronous Serial Model for the Feasa LED Analyser
!
! The sequence of this test is as follows
! - Initialise all the inputs
!       - Set Addr_0 to [0]
!       - Set Ser_clk to [0]
!       - Set Ser_in     to [0]
! - Reset the Synchronous serial communications by
!       - Set Addr_0 to [0]
!       - Set Ser_in     to [1]
!       - Execute Ser_clk [1] then Ser_clk[0] at leaset 8 times
! - Sent the Set_exposure_factor [0X40]  command as follows
!       - Clock in the following 8 bits 01000000
!       - Set Ser_input  to [0]
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Set Ser_input  to [1]
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until all 8 data bits are clocked in
! - Send the exposure factor from 1 to 15 as follows
!       - Set Ser_input to the MSB of the exposure factor
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - Continue until the 8 bits has been clocked in
! - Read back the exposure factor which has just been programmed to verify
!   that the command has been succesful
!       - Set Ser_clk to [1]
!       - Set Ser_clk to [0]
!       - The the Ser_outp pin (Msb is output first)
!       - Repeat 8 times until all data bits have been read
!       - The byte which is read back shoud be the same as the exposure factor


!*******************************************************************************
test digital; Sen(*),Data(*)
sequential

test time 2000m

vector cycle  50u
receive delay 49u

set terminators to on

assign Ser_out            to pins "Ser_out"
assign Ser_in             to pins "Ser_in"
assign Ser_clk            to pins "Ser_clk"

assign GND1               to pins "GND1"

assign Reset              to pins "Reset"

assign OE_bar             to pins "OE_bar"
assign Ext_trig           to pins "Ext_trig"

assign PWM                to pins "PWM"

assign Addr_0             to pins "Addr_0"
assign Addr_1             to pins "Addr_1"
assign Addr_2             to pins "Addr_2"
assign Addr_3             to pins "Addr_3"
assign Addr_4             to pins "Addr_4"

assign WE_bar             to pins "WE_bar"
assign Int                to pins "Int"
assign Color              to pins "Color"
assign Sat                to pins "Sat"

assign RyBy_bar           to pins "RyBy_bar"

assign VCC                to pins "VCC"
assign GND                to pins "GND"

power  VCC, GND, GND1

family   CMOS_3V3

inputs   Ser_in, Ser_clk, Addr_0
outputs  Ser_out, RyBy_bar

nondigital Int, Color, Sat, OE_bar, Ext_trig, PWM
nondigital Addr_1, Addr_2, Addr_3, Addr_4, WE_bar, Reset

!********** assignments **********************

variable Sen(*) to groups Ser_in

capture Ser_out pack 8    !8 data bits in each byte read back from Analyser

!*******************************************************************
!*******************      VECTOR SECTION       *********************
!*******************************************************************

vector  Set_values     ! set LED Analyser to known state
   set Addr_0          to "0"
   set Ser_clk         to "0"
   set Ser_in          to "0"
end vector

vector Keep_values     ! Save input values.
   set Addr_0          to "k"
   set Ser_clk         to "k"
   set Ser_in          to "k"
end vector

vector Addr_0_high
   initialize to Keep_values
   set Addr_0          to "1"
end vector

vector Addr_0_low
   initialize to Keep_values
   set Addr_0          to "0"
end vector

vector Clock
   initialize to Keep_values
   set Ser_clk         to "t"
end vector

vector Clock_1
   initialize to Keep_values
   set Ser_clk         to "1"
end vector

vector Clock_0
   initialize to Keep_values
   set Ser_clk         to "0"
end vector

vector Start_bit
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_in_X
   initialize to Keep_values
   set Ser_in          to "x"
end vector

vector Ser_in_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector In_0
   initialize to Keep_values
   set Ser_in          to "0"
end vector

vector Ser_in_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector In_1
   initialize to Keep_values
   set Ser_in          to "1"
end vector

vector Ser_out_x
   initialize to Keep_values
   set Ser_out         to "x"
end vector

vector Ser_out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Out_0
   initialize to Keep_values
   set Ser_out         to "0"
end vector

vector Ser_out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector

vector Out_1
   initialize to Keep_values
   set Ser_out         to "1"
end vector


!*******************************************************************
!*******************    SUBROUTINE SECTION     *********************
!*******************************************************************

! Command to reset or initialize Communications
! Send 11111111 (FF) to LA
sub Reset_Comms
   execute Addr_0_low                  !Chip Select = 0
   execute Clock_0
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_1                    !Extra clocks
   execute Clock
   execute Clock
   execute Ser_in_1                    !Extra Clocks
   execute Clock
   execute Clock
end sub

! Command to change the test sensitivity
! Send 01000000 to LA
sub Set_sensitivity
   execute Addr_0_high                 !Chip Select = 1
   execute Clock_0
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_1                    !1
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
   execute Ser_in_0                    !0
   execute Clock
   execute Clock
end sub

! Read 8 data bits from Ser_out pin
! The pack 8 statement has been used so the 8 data bits will
! be read back as one Byte
sub Read_Capture

   repeat 8 times
   execute Clock
   execute Clock
   execute Ser_out_x capture  ! Captures a bit will be put in array using fetch command
   end repeat

end sub

!*******************************************************************
!*********************      UNIT SECTION       *********************
!*******************************************************************


!---------------------------------------------------------------------
!  Initialize all the inputs to a known value
!------------------------------------------------------------------
unit "Initialize All"
    execute Set_values
end unit

!------------------------------------------------------------------
!  Clear the input buffer of the Led Analyser to
!  ensure that the led analyser has detected no false
!  clock inputs
!-------------------------------------------------------------------------
unit "Reset Communications"
        call Reset_Comms                        !Send Reset Comms Code x0FF
        wait 100u
end unit

!-------------------------------------------------------------------------
! This sends the Set Test Sensitivity command to the Led Analyser
!--------------------------------------------------------------------------
unit "Send Set Test Sensitivity"
   call Set_Sensitivity      ! Send Set Sensitivity - Code x040
end unit

!--------------------------------------------------------------------------
!   Unit to Set the Sensitivity Factor
!--------------------------------------------------------------------------
! The Sensitivity/Exposure Factor is passed into the test using the Array Sen(*)
!   The MSB is passed in first so the format is as follows
!   Sen(0) = MSB (Bit7)
!   Sen(1) = Bit6
!   Sen(2) = Bit5
!   Sen(3) = Bit4
!   Sen(4) = Bit3
!   Sen(5) = Bit2
!   Sen(6) = Bit1
!   Sen(7) = LSB (Bit0)
! i.e. to set factor 01, the binary code is 00000001, set Sen(7) to 1 and all others to 0
!----------------------------------------------------------------------

unit "Set Factor"
   execute Ser_in_x drive Sen(0)    !MSB
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(1)    !Bit6
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(2)    !Bit5
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(3)    !Bit4
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(4)    !Bit3
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(5)    !Bit2
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(6)    !Bit1
   execute Clock
   execute Clock
   execute Ser_in_x drive Sen(7)    !Bit0
   execute Clock
   execute Clock
   wait 50m   ! need to wait 50ms as the Led Analyser performs calculations
end unit

!
! End of test
!


